package com.weapons.week0903_weapons.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.bean.RecommentBean;
import com.weapons.week0903_weapons.view.RecommentTopView;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Administrator on 2016/7/20.
 */
public class RecommendListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private RecommentTopView mHeadView;
    private List<RecommentBean.ItemBean> mDatas;
    private int headViewSize = 0;
    private RecommentTopView topView;
    private int mHeaderCount = 1;
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1;

    public void addHeadView(View view) {
        this.mHeadView = (RecommentTopView) view;
        headViewSize = 1;
    }
    // 内容长度
    public int getContentItemCount() {
        return mDatas.size();
    }
    /**
     * 判断当前item是否是headview
     *
     * @param position
     * @return
     */
    public boolean isHeaderView(int position) {
        return mHeaderCount != 0 && position < mHeaderCount;
    }

    @Override
    public int getItemViewType(int position) {
        int dataItemCount = getContentItemCount();
        if (mHeaderCount != 0 && position < mHeaderCount) {
            return TYPE_HEADER;
        } else {
            //内容View
            return TYPE_ITEM;
        }
    }


    public RecommendListAdapter(Context context, List<RecommentBean.ItemBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                view = mHeadView;
                holder = new HeaderHolder(view);
                break;
            case 1:
                view = LayoutInflater.from(mContext).inflate(R.layout.recomment_item, null);
                holder = new ItemHoler(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HeaderHolder) {
        } else {
            Picasso.with(mContext).load(mDatas.get(position-1).getImage()).into(((ItemHoler)holder).iv_itemImage);
            ((ItemHoler)holder).tv_title.setText(mDatas.get(position-1).getTitle());
            ((ItemHoler)holder).tv_time.setText(mDatas.get(position-1).getPubDate());
            ((ItemHoler)holder).tv_comment.setText(mDatas.get(position-1).getComments() + "评论");
            if(mListener!=null){
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mListener.onItemClick(position,holder.itemView);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return  mDatas.size()+1;
    }

    class HeaderHolder extends RecyclerView.ViewHolder {

        public HeaderHolder(View itemView) {
            super(itemView);
        }
    }

    class ItemHoler extends RecyclerView.ViewHolder {
        private ImageView iv_itemImage;
        private TextView tv_title, tv_time, tv_comment;

        public ItemHoler(View itemView) {
            super(itemView);
            iv_itemImage = (ImageView) itemView.findViewById(R.id.iv_itemImage);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_comment);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }
    public interface OnItemClickListener{
        void onItemClick(int pos ,View itemView);
    }
    public OnItemClickListener mListener;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListener = listener;
    }
}
